<?php

namespace App\Listeners;

use App\Events\MoneyChangeEvent;
use App\Models\Fund;
use App\Models\MoneyLog;
use App\Models\Stock;
use App\Models\User;
use App\Models\UserLevel;
use Carbon\Carbon;

class ListChangeListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  MoneyChangeEvent $event
     * @return void
     */
    public function handle(MoneyChangeEvent $event)
    {
        //
//        dd('...');
        if ($event->type == 'fund') {
            $fund = Fund::whereOrderNo($event->order_no)->where('user_id', $event->buy_user)->where('status', 1)->where('send_status', 0)->first();
            if ($fund) {
                $key = 'fund_point_' . $fund->year;
                $money = $fund->num * 10000 * config($key) / 12 * 0.01;
                $double = $fund->num * 10000 * config("fund_point_double") / 12 * 0.01;

                for ($i = 1; $i <= 4 * $fund->year; $i++) {
                    $month = Carbon::now();
                    $pay_at = $month->addMonth($i * 3 - 2);
                    $create['type'] = 'fund_month';
                    $create['money'] = $i == 1 ? 3 * ($money + $double) : $money * 3;
                    $create['pay_at'] = $pay_at;
                    $create['pay_status'] = 0;
                    $create['money_no'] = "M" . date('YmdHis') . rand(1000, 9999);
                    $create['order_no'] = $fund->order_no;
                    $create['buy_user'] = $event->buy_user;
                    $create['user_id'] = $event->buy_user;
                    $create['change_type'] = true;
                    $create['name'] = '基金' . $fund->year . '年期' . '第' . $i . '次收益';
                    $log = MoneyLog::create($create);
                }
                $fund->send_status = 1;
                $fund->save();
            }

        }

    }

    public function checkRadio(User $user, MoneyChangeEvent $event, $type = 'fund')
    {

    }

}
